<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="450px" :title="`${dialogProps.title}服务商资料`">
    <el-form
      ref="ruleFormRef"
      label-width="120px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="编码" prop="number">
        <el-input v-model="dialogProps.row!.number" disabled />
      </el-form-item>
      <el-form-item label="帐号余额" prop="balance">
        <el-input v-model="dialogProps.row!.balance" disabled />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dialogProps.row!.name" clearable />
      </el-form-item>
      <el-form-item label="联系人" prop="linkMan">
        <el-input v-model="dialogProps.row!.linkMan" clearable />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="dialogProps.row!.mobile" clearable />
      </el-form-item>
      <!-- <el-form-item label="分服务器" prop="isCreateFacCode">
        <el-select class="w100" v-model="dialogProps.row!.isCreateFacCode">
          <el-option label="创建" :value="true" />
          <el-option label="不创建" :value="false" />
        </el-select>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ServiceDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Operation } from "@/api/interface";
const rules = reactive({
  name: [{ required: true, message: "请填写服务商名称" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Operation.ResDepartmentList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.title}服务商成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
